package com.tecent.chat.actvity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.tecent.chat.R;
import com.tecent.chat.po.MyContacts;
import com.tecent.chat.utils.ContactUtils;

import java.util.ArrayList;

public class contentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

        Button btn1 = findViewById(R.id.rper);
        Button btn2 = findViewById(R.id.rper2);
        bindClick1(btn1);
        bindClick2(btn2);

        // 判断是否有通讯录权限,有权限就读取，没有就请求权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
            readContacts();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.READ_CONTACTS
            }, 2);
        }
    }

    // 打开通讯录
    private void openConcacts(){
        Intent intent = new Intent();
        intent.setAction("android.intent.action.PICK");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setType("vnd.android.cursor.dir/phone_v2");
        startActivityForResult(intent, 0x30);
    }


    private void readContacts() {
        System.out.println("read.....");
        ArrayList<MyContacts> list1 = ContactUtils.getAllContacts(this);
        System.out.println(list1);
    }


    public void bindClick1(Button btn) {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(contentActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                    call();
                } else {
                    ActivityCompat.requestPermissions(contentActivity.this, new String[]{
                            Manifest.permission.CALL_PHONE
                    }, 1);
                }
            }
        });
    }

    public void bindClick2(Button btn) {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              openConcacts();
            }
        });
    }


    // 打电话
    private void call() {
        Toast.makeText(contentActivity.this, "xiaohua", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:10086"));
        startActivity(intent);
    }

    // 请求权限的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    call();
                } else {
                    Toast.makeText(contentActivity.this, "您拒绝了电话权限哦", Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts();
                } else {
                    Toast.makeText(contentActivity.this, "您拒绝了通讯录权限哦", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}
